package com.lovo.dao;

import java.sql.*;
import java.util.Optional;

public class BaseDAO {
    protected Connection con;
    public Connection getConnection(){
        try {
            // 1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.获取连接
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fronttoback?" +
                    "userSSL=true&useUnicode=true&" +
                    "characterEncoding=UTF8&serverTimezone=GMT","root","lovo");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    public void close(Connection con, Statement statement){
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }
    public void close(Connection con, Statement statement, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
      close(con,statement);
    }

    public static void main(String[] args) {
        BaseDAO baseDAO = new BaseDAO();
        System.out.println(baseDAO.getConnection());
    }

}
